﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business;
using Microsoft.Practices.Composite.Events;
using PJ.Business.Inventory;
using PJ.Data;

namespace PJ.Presentation.Inventory
{
    /// <summary>
    /// Inventory item list model
    /// </summary>
    public class InventoryItemListModel : BusinessObjectListViewModel<IInventoryItemInfoModel>, IInventoryItemListModel
    {
        #region Constructor
        /// <summary>
        /// Initialize
        /// </summary>
        public InventoryItemListModel()
        {
        }
        /// <summary>
        /// Initialize
        /// </summary>
        /// <param name="view">view</param>
        /// <param name="aggregator">aggregator</param>
        /// <param name="service">service</param>
        [Microsoft.Practices.Unity.InjectionConstructor]
        public InventoryItemListModel(IInventoryItemListView view, IEventAggregator aggregator, IInventoryItemService service)
            : base(view, aggregator, service)
        {
            EventAggregator.GetEvent<InventoryItemSearchResultChangeEvent>().Subscribe(this.SetValues);
        }
        #endregion

        #region Protected

        /// <summary>
        /// Create new item
        /// </summary>
        /// <param name="globalId">Global id of object to initialize from</param>
        /// <returns></returns>
        protected override IBusinessObjectModel CreateNewItem(string globalId)
        {
            InventoryItemInfoModel model = new InventoryItemInfoModel(globalId, (IInventoryItemService)Service);
            model.SetValues(Service.GetData(globalId, GetDataMode.UseCache), false);
            return model;
        }
        /// <summary>
        /// Create new item
        /// </summary>
        /// <param name="obj">Business object to initialize from</param>
        /// <returns></returns>
        protected override IBusinessObjectModel CreateNewItem(IBusinessObject obj)
        {
            return new InventoryItemInfoModel(obj, (IInventoryItemService)Service);
        }
        #endregion

        #region Field

        #endregion


        /// <summary>
        /// Selection change handler
        /// </summary>
        /// <param name="businessObject"></param>
        public override void SelectionChange(IBusinessObjectModel businessObject)
        {
            if (businessObject == null)
                EventAggregator.GetEvent<InventoryItemSelectionEvent>().Publish(null);
            else
                EventAggregator.GetEvent<InventoryItemSelectionEvent>().Publish(
                    new EntityEventArg() { GlobalId = businessObject.GlobalId, Id = ((IEntityModel)businessObject).Id });
        }

        /// <summary>
        /// Initialize new item handler
        /// </summary>
        /// <param name="newItem"></param>
        public override void InitializingNewItem(IBusinessObjectModel newItem)
        {
            throw new NotImplementedException();
        }
    }
}
